<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "xuanxiu";

// 创建数据库连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 查询每个部门的员工数量和性别统计
$sql = "SELECT d.department,COUNT(e.id) AS total_employees,
            SUM(CASE WHEN e.gender = '0' THEN 1 ELSE 0 END) AS male_count,
            SUM(CASE WHEN e.gender = '1' THEN 1 ELSE 0 END) AS female_count
        FROM 
            employees e
        LEFT JOIN 
            department d ON e.depart_id = d.id
        GROUP BY 
            d.department";
$result = $conn->query($sql);

// 准备以JSON格式输出的数据
$data = [];
while ($row = $result->fetch_assoc()) {
    $data[] = [
        'department' => $row['department'],
        'totalEmployees' => (int)$row['total_employees'],
        'maleCount' => (int)$row['male_count'],
        'femaleCount' => (int)$row['female_count']
    ];
}

// 查询某员工的工资数据
$employeeId = 1; // 示例员工ID
$sqlSalary = "SELECT date, salary FROM salary WHERE employee_id = $employeeId ORDER BY date";
$resultSalary = $conn->query($sqlSalary);

// 准备以JSON格式输出的工资数据
$salaryData = [];
while ($row = $resultSalary->fetch_assoc()) {
    $salaryData[] = [
        'date' => $row['date'],
        'salary' => (float)$row['salary']
    ];
}

// 关闭数据库连接
$conn->close();

// 将数据编码为JSON格式，用于AJAX响应
header('Content-Type: application/json');
echo json_encode(['departmentData' => $data, 'salaryData' => $salaryData]);
?>
